package com.dong.mcd.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dong.mcd.model.SysMenu;
import com.dong.mcd.model.SysRole;
import com.dong.mcd.model.SysUser;
import com.dong.mcd.param.SysMenuParam;
import com.dong.mcd.pub.BaseAction;
import com.dong.mcd.pub.Constants;
import com.dong.mcd.service.IMenuService;
import com.dong.mcd.util.JSONUtil;

@Controller
@RequestMapping("/system/menu")
public class MenuAction extends BaseAction {

	@Autowired
	private IMenuService menuService;
	
	/**
	 * 查询菜单
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/query")
	public void queryMenu(HttpServletRequest request, HttpServletResponse response,SysMenuParam param) throws Exception {
		List<SysMenu> menuList= menuService.query(param);
		writeJson(response, menuList);
	}
	
	@RequestMapping("/queryParent")
	public void queryParentMenu(HttpServletRequest request, HttpServletResponse response) throws Exception {
		SysMenuParam param = new SysMenuParam();
		param.setParentId("-1");
		List<SysMenu> menuList= menuService.query(param);
		SysMenu menu = new SysMenu();
		menu.setId("-1");
		menu.setName("无");
		menuList.add(0, menu);
		writeJson(response, menuList);
	}
	
	@RequestMapping("/page")
	public void queryMenuPage(HttpServletRequest request, HttpServletResponse response,SysMenuParam param) throws Exception {
		
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		writeJson(response, menuService.queryPage(param, pageIndex, pageSize).getPageMap());
	}
	
	@RequestMapping("/modify")
	public void modifyMenu(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String menuStr = request.getParameter("jsonData");
		try {
			SysMenu menu = JSONUtil.toObject(menuStr, SysMenu.class);
			if (StringUtils.isEmpty(menu.getId())){
				writeError(response, "菜单编号不能为空");
				return;
			}
			if (StringUtils.isEmpty(menu.getName())){
				writeError(response, "菜单名称不能为空");
				return;
			}
			menuService.modifyMenu(menu);
			writeSuccess(response, "修改成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/add")
	public void addMenu(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String menuStr = request.getParameter("jsonData");
		try {
			SysMenu menu = JSONUtil.toObject(menuStr, SysMenu.class);
			if (StringUtils.isEmpty(menu.getId())){
				writeError(response, "菜单编号不能为空");
				return;
			}
			if (StringUtils.isEmpty(menu.getName())){
				writeError(response, "菜单名称不能为空");
				return;
			}
			if (menuService.getMenuById(menu.getId()) != null){
				writeError(response, "菜单编号已存在");
				return;
			}
			menuService.addMenu(menu);
			writeSuccess(response, "添加成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/delete")
	public void deleteMenu(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		try {
				menuService.removeMenu(id);
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	/**
	 * 得到用户菜单
	 */
	@RequestMapping("/getUserMenus")
	public void getUserMenus(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpSession session = request.getSession();
		SysUser curUser = (SysUser)session.getAttribute(Constants.CURRENT_USER);
		if (curUser == null){
			curUser = new SysUser();
			curUser.setId(1);
		}
		List<SysMenu> menuList= menuService.getUserMenus(curUser.getId());
		writeJson(response, menuList);
	}
	
	@RequestMapping("/getRoleMenus")
	public void getRoleMenus(HttpServletRequest request, HttpServletResponse response,SysRole role) throws Exception {
		writeJson(response, menuService.getRoleMenusAll(role.getId()));
	}
	
}
